package cate.game.activity.lostdreamland;

import cate.common.table.d.GDFunc;
import cate.game.activity.base.core.Activity;
import cate.game.activity.base.core.ActivityHandler;
import cate.game.activity.lostdreamland.po.LostDreamlandRecord;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Activity(relatedFuncIds = {GDFunc.LOST_DREAMLAND})
public class LostDreamlandHandler extends ActivityHandler<LostDreamlandSPO, LostDreamlandActivity> {
    @NoteField(value = "剧本记录")
    private final Map<Integer, Map<String, List<PlayPO>>> playMap = new HashMap<>();
    @Override
    public String getName() {
        return "迷失幻境";
    }

    @Override
    public LostDreamlandSPO buildGlobal() {
        return new LostDreamlandSPO();
    }

    @Override
    public LostDreamlandActivity buildSingle() {
        return new LostDreamlandActivity();
    }

    @Override
    public void onActOpen() {

    }

    @Override
    public void onActClose() {

    }

    public List<PlayPO> tryUpdate(Role role, PlayPO play, int day, int level) {
        Map<String, List<PlayPO>> rolePlays = forceGetRolePlays(role);
        if (rolePlays == null) {
            return new ArrayList<>();
        }
        LostDreamlandActivity e = role.getActivity().getActivitySingle(getFuncId());
        if (e == null || !e.enable()) {
            return new ArrayList<>();
        }
        List<PlayPO> plays = rolePlays.computeIfAbsent(role.getUid(), k -> new ArrayList<>());
        plays.add(play);
        this.getRecord(e.activityId(), day, level).tryRecord(role, plays);
        rolePlays = forceGetRolePlays(role);
        if (rolePlays != null && !rolePlays.isEmpty()) {
            synchronized (playMap) {
                rolePlays.remove(role.getUid());
            }
            log.info("迷失幻境模块【正常结束】 释放了一个玩家的录像 当前缓存了{}个玩家的录像", playMap.keySet().size());
        }
        return plays;
    }

    private Map<String, List<PlayPO>> forceGetRolePlays(Role role) {
        LostDreamlandActivity e = role.getActivity().getActivitySingle(getFuncId());
        if (e == null || !e.enable()) {
            return null;
        }
        Map<String, List<PlayPO>> activityPlays;
        synchronized (playMap) {
            activityPlays = playMap.computeIfAbsent(e.activityId(), k -> new HashMap<>());
        }
        return activityPlays;
    }

    public LostDreamlandRecord getRecord(int activityId, int day, int level) {
        return getGlobalData().getRecord(activityId, day, level);
    }

    public void recordPlay(Role role, PlayPO play) {
        Map<String, List<PlayPO>> rolePlays = forceGetRolePlays(role);
        if (rolePlays == null) {
            return;
        }
        rolePlays.computeIfAbsent(role.getUid(), k -> new ArrayList<>()).add(play);
        log.info("迷失幻境模块【战斗结算】 缓存了一场录像 当前缓存了{}个玩家的录像", rolePlays.keySet().size());
    }

    public List<PlayPO> removePlay(Role role) {
        final Map<String, List<PlayPO>> rolePlays = getRolePlays(role);
        if (rolePlays == null) {
            return new ArrayList<>();
        }
        List<PlayPO> plays;
        synchronized (playMap) {
            plays = rolePlays.remove(role.getUid());
            if (plays == null) {
                plays = new ArrayList<>();
            }
            log.info("迷失幻境模块【战斗失败】 手动释放了一个玩家的录像 当前缓存了{}个玩家的录像", playMap.keySet().size());
        }
        return plays;
    }

    private Map<String, List<PlayPO>> getRolePlays(Role role) {
        LostDreamlandActivity e = role.getActivity().getActivitySingle(getFuncId());
        if (e == null || !e.enable()) {
            return null;
        }
        Map<String, List<PlayPO>> plays;
        synchronized (playMap) {
            plays = playMap.get(e.activityId());
        }
        return plays;
    }

}
